package com.yzt.zhuju.base;

import android.content.Context;
import android.util.Log;

import com.baidu.location.LocationClient;
import com.baidu.mapapi.CoordType;
import com.baidu.mapapi.SDKInitializer;
import com.danikula.videocache.HttpProxyCacheServer;
import com.yzt.zhuju.application.BaseApp;
import com.yzt.zhuju.arouter.BaseRouter;
import com.yzt.zhuju.config.AppConfig;
import com.yzt.zhuju.utils.CacheUtils;
import com.yzt.zhuju.utils.Utils;
import com.yzt.zhuju.utils.map.BdLocationHelper;
import com.yzt.zhuju.utils.map.MapHelper;

/**
 * author : bob
 * date   : 2022/11/29
 * desc   :
 */
public class BaseApplication extends BaseApp {

    private HttpProxyCacheServer proxy;

    /**
     * 位于前台的 Activity 的数目
     */
    protected int foregroundCount = 0;

    @Override
    public void onCreate() {
        super.onCreate();
        Utils.init(this);
        BaseRouter.getInstance().init(this);
        // 初始化地图
        initMap();
        // 初始化定位
        getBdLocationHelper();
    }

    /**
     * 初始化地图
     */
    public void initMap() {
        SDKInitializer.setAgreePrivacy(Utils.getApp(), true);
        LocationClient.setAgreePrivacy(true);
        // 默认本地个性化地图初始化方法
        SDKInitializer.initialize(Utils.getApp());
        // 包括BD09LL和GCJ02两种坐标，默认是BD09LL坐标。
        SDKInitializer.setCoordType(CoordType.BD09LL);
        MapHelper.initContext(Utils.getApp());
        MapHelper.setMapType(MapHelper.MapType.BAIDU);
    }

    public static HttpProxyCacheServer getVideoCacheProxy(Context context) {
        BaseApplication app = (BaseApplication) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer.Builder(this)
                .cacheDirectory(CacheUtils.getVideoCacheDir(this))
                .build();
    }

    private static BdLocationHelper mBdLocationHelper;

    public void getBdLocationHelper() {
        try {
            if (mBdLocationHelper == null)
                mBdLocationHelper = new BdLocationHelper(this);
        } catch (Exception e) {

            Log.e("mBdLocationHelper","mBdLocationHelper---------->"+e.getMessage());
        }
    }

    public static BdLocationHelper getLocalHelper() {
        return mBdLocationHelper;
    }

    public static boolean isAppForeground(Context context) {
        return ((BaseApplication) AppConfig.application).isAppForeground();
    }

    /**
     * 是否处于前台环境
     */
    private boolean isAppForeground() {
        return foregroundCount > 0;
    }
}
